#ifndef ML_MESH_H
#define ML_MESH_H

#include <ode/ode.h>

// Context

#ifdef JMBC_INSIDE

#include <Core/clCore.h>

typedef struct
{
	int magic;
	unsigned int vertices;
	unsigned int materials;
	unsigned int faces;
	unsigned int tags;
} MLMeshHeader;

typedef struct
{
	float coord[3];
	float normal[3];
} MLMeshVertex;

typedef struct
{
	wchar_t name[TL_STRING_DEFAULT_FIXED_SIZE];
	unsigned int start;
	unsigned int faces;
} MLMeshMaterial;

typedef struct
{
	unsigned int vertices[3];
	float normal[3];
	float texcoords[3][2];
} MLMeshFace;

typedef struct
{
	wchar_t name[TL_STRING_DEFAULT_FIXED_SIZE];
	float matrix[16];
} MLMeshTag;

#define ML_MESH_GROUP_VERTICES 0
#define ML_MESH_GROUP_MATERIALS 1
#define ML_MESH_GROUP_FACES 2
#define ML_MESH_GROUP_TAGS 3

typedef struct
{
	unsigned int groups[4];

	unsigned int materials;

	dTriMeshDataID trimesh;
	dGeomID geom;

	float matrix[16];
} MLMesh;

#endif

void
mlMeshCreateContext(void);

// Generation / Deletion

void
mlGenMeshes(unsigned int n, unsigned int *meshes);

void
mlDeleteMeshes(unsigned int n, unsigned int *meshes);

// Load / Unload

void
mlMeshLoad(unsigned int id, unsigned int filepath);

void
mlMeshUnload(unsigned int id);

void
mlMeshesUnload(unsigned int n, unsigned int *meshes);

// Operation

void
mlMeshCompile(unsigned int id, unsigned int filepath);

void
mlMeshDecompile(unsigned int id, unsigned int filepath);

unsigned int
mlMeshVertices(unsigned int id);

unsigned int
mlMeshMaterials(unsigned int id);

unsigned int
mlMeshMaterial(unsigned int id, unsigned int name);

unsigned int
mlMeshMaterialMaterial(unsigned int id, unsigned int material);

unsigned int
mlMeshFaces(unsigned int id);

unsigned int
mlMeshTags(unsigned int id);

unsigned int
mlMeshTag(unsigned int id, unsigned int tagname);

void
mlMeshTagMatrix(unsigned int id, unsigned int tag, float matrix[16]);

float *
mlMeshMatrix(unsigned int id);

// Disk I/O

void
mlMeshSave(unsigned int id, unsigned int filepath);

void
mlMeshGenMaterials(unsigned int id, unsigned int path);

void
mlMeshDeleteMaterials(unsigned int id);

// OpenGL Integration

void
mlMeshDraw(unsigned int id);

//void
//mlMeshFrustumDraw(unsigned int id, unsigned int textures, unsigned int frustum);

// ODE Integration

void
mlMeshCreateGeom(unsigned int id, dSpaceID space);

void
mlMeshDestroyGeom(unsigned int id);

#endif
